// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-16 09:35:57
 * @LastEditors: qiulin ql1746365080@gmail.com
 * @LastEditTime: 2025-01-10 16:48:38
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Property\PSOILDParser.cpp
 *
 */

#include "PSOLIDParser.h"

#include <algorithm>
#include <cstdint>

#include <boost/algorithm/string.hpp>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Property/IPSOLIDService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (PSOLID);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/PSOLID/PSOLID.md>
*/
// 以下需要修改
enum PSOLIDkey : std::uint8_t
{
    PSOLIDKEY_PID    = 2,
    PSOLIDKEY_MID    = 3,
    PSOLIDKEY_CORDM  = 4,
    PSOLIDKEY_IN     = 5,
    PSOLIDKEY_STRESS = 6,
    PSOLIDKEY_ISOP   = 7,
    PSOLIDKEY_FCTN   = 8
};
SG::DataStructure::Common::Status BDF::BULKDATA::ParsePSOLID (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                              std::vector<std::string>&                               dividedEntryInfo)
{
    // 初始化临时的PSOLID数据结构，用于存储解析后的数据
    SG::DataStructure::FEM::PSOLIDData tempPSOLID;

    // TODO(bug 001) m_domainId的正确填写，目前默认设置为1
    tempPSOLID.m_domainId = 1;

    int curIDKey = 1;  // 当前处理的字段索引

    // 遍历分割后的字符串条目
    for (auto curStrKey : dividedEntryInfo)
    {
        // 将字符串转换为科学计数法

        // 检查当前字符串是否为空（只包含空格）
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curIDKey++;  // 跳过空字段，继续下一个字段
            continue;
        }

        // 根据当前字段索引解析相应的数据并存储到tempPSOLID
        switch (curIDKey)
        {
        case PSOLIDKEY_PID:
            // 解析PID字段，将其转换为长整型并存储到m_id
            tempPSOLID.m_id = convertToI64 (curStrKey);
            break;
        case PSOLIDKEY_MID:
            // 解析MID字段，将其转换为长整型并存储到m_mId
            tempPSOLID.m_mId = convertToI64 (curStrKey);
            break;
        case PSOLIDKEY_CORDM:
            // 解析坐标系ID（CORDM），将其转换为长整型并存储到m_mCId
            tempPSOLID.m_mCId = convertToI64 (curStrKey);
            break;
        case PSOLIDKEY_IN:
            // 解析IN字段，将其转换为长整型并存储到m_in
            boost::trim (curStrKey);
            if (curStrKey.size () > 1)
            {
                switch (curStrKey[1])
                {
                case 'H':  // THREE
                case 'h':
                    tempPSOLID.m_in = 3;
                    break;
                case 'W':  // TWO
                case 'w':
                    tempPSOLID.m_in = 2;
                    break;
                default:  // BUBBLE
                    tempPSOLID.m_in = 0;
                    break;
                }
            }
            else
            {
                tempPSOLID.m_in = convertToI64 (curStrKey);
            }

            break;
        case PSOLIDKEY_STRESS:
            boost::trim (curStrKey);
            if (curStrKey.size () > 1)
            {
                switch (curStrKey[1])
                {
                case 'a':  // GAUSS
                case 'A':
                    tempPSOLID.m_stress = 1;
                    break;
                default:  // GRID
                    tempPSOLID.m_stress = 0;
                    break;
                }
            }
            else
            {
                tempPSOLID.m_stress = convertToI64 (curStrKey);
            }
            break;
        case PSOLIDKEY_ISOP:
            boost::trim (curStrKey);
            if (curStrKey.size () > 1)
            {
                switch (curStrKey[1])
                {
                case 'U':  // FULL
                case 'u':
                    tempPSOLID.m_iosp = 1;
                    break;
                default:  // REDUCED
                    tempPSOLID.m_iosp = 0;
                    break;
                }
            }
            else
            {
                tempPSOLID.m_iosp = convertToI64 (curStrKey);
            }

            break;
        case PSOLIDKEY_FCTN:
            // 解析FCTN字段，将其前4个字符存储到m_fctn数组中
            for (size_t i = 0; i < 4; i++)
            {
                tempPSOLID.m_fctn[i] = curStrKey[i];
            }
            break;
        default:
            // 如果索引不匹配任何已知字段，跳过
            break;
        }

        curIDKey++;  // 处理完当前字段，移动到下一个字段
    }

    // 将解析后的PSOLID数据添加到数据库服务中
    return dbServiceFactory->get<IPSOLIDService> ()->Add (tempPSOLID);
}